<?php
//session_start();
require_once("twitteroauth/twitteroauth.php");
require_once("configuration.php");
include('header.php');

include('gettweets.php');

//verify the response from twitter
if(!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret'])){
    // We've got everything we need
    
    //echo '<section class="default">';
    //echo "You have succesfully registered our application on your twitter! <br>";
    
	// TwitterOAuth instance, with two new parameters we got in twitter_login.php
	$twitteroauth = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
	// Let's request the access token
	$access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);
	// Save it in a session var
	$_SESSION['access_token'] = $access_token;
	//var_dump($_SESSION);
	echo "<br>";
	//echo $_SESSION['username'];
	echo "<br>";
	//var_dump($_COOKIE['streamtube_cookie']);

	// Let's get the user's info
	$user_info = $twitteroauth->get('account/verify_credentials');
	// Print user's info
	var_dump($access_token);
	echo "<br>User info:<br>";
	var_dump($user_info);
	var_dump($user_info->screen_name);
	
	//authenticate user in the session
	$_SESSION['username']=$user_info->screen_name;
	$_SESSION['user_oauth_token']=$access_token['oauth_token'];
	$_SESSION['user_oauth_token_secret']=$access_token['oauth_token_secret'];
	$_SESSION['logged_in']=True;
	echo "<br>session:<br>";
	var_dump($_SESSION);
	echo '<script>window.location = "http://arc.inf.unisi.ch/twbe/youtube/player.php"</script>';
//echo "</body>";
}
?>